package com.ccrfid.rmc6.pojo.param;

import org.springframework.data.domain.Sort;

import javax.ws.rs.QueryParam;
import java.util.ArrayList;
import java.util.List;

public class SortParam {
    private List<Sort.Order> orders = new ArrayList<>();

    public SortParam(@QueryParam("sort") List<String> sortList) {
        for (String sortKV : sortList) {
            String[] kv = sortKV.split(",");
            if (kv.length == 2) {
                String property = kv[0];
                String direction = kv[1].toUpperCase();
                if (direction.equals("ASC")) {
                    orders.add(new Sort.Order(Sort.Direction.ASC, property));
                } else if (direction.equals("DESC")) {
                    orders.add(new Sort.Order(Sort.Direction.DESC, property));
                }
            }
        }
    }

    public List<Sort.Order> getOrders() {
        return orders;
    }

    public void setOrders(List<Sort.Order> orders) {
        this.orders = orders;
    }
}
